package com.google.code.jtasks.server.resources;

import com.google.code.jtasks.common.data.User;
import com.google.code.jtasks.server.dao.TokenManagers.DefaultTokenManager;
import com.google.code.jtasks.server.dao.TokenManagers.TokenManager;
import com.google.code.jtasks.server.dao.UserDAO;
import com.google.code.jtasks.server.modules.BindingDBUsers;
import com.google.common.base.Optional;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Request;
import java.util.Collection;

/**
 * Created with IntelliJ IDEA.
 * User: a c e r
 * Date: 15.05.13
 * Time: 18:01
 * To change this template use File | Settings | File Templates.
 */
@Path("/users")
public class UserResource {
    private static final Logger LOG = LoggerFactory.getLogger(UserResource.class);

    private final TokenManager tokenGenerate;
    private final UserDAO userDAO;

    @Inject

    public UserResource(TokenManager tokenGenerate, @BindingDBUsers UserDAO userDAO) {
        this.tokenGenerate = tokenGenerate;
        this.userDAO = userDAO;
    }


    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{name}")

    public Response getUser(@PathParam("name") String userName) {
        LOG.debug("Get User request received: User name {}", userName);
        Optional<User> userOptional = userDAO.loadUser(userName);
        if (userOptional.isPresent()) {
            tokenGenerate.setUserName(userName);
             return Response.ok(tokenGenerate.getMap()).build();
        } else {
            return Response.status(404).build();
        }
    }
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Collection<User> userList() {
        LOG.debug("Get user list request received");
        return userDAO.loadAllUsers();
    }







}
